package core.command;

import java.util.ArrayList;

import core.Drawing;
import core.Point2D;
import core.Stroke;
import core.Vector2D;

public class TranslateStrokesCommand extends AbstractCommand {

	private ArrayList<Stroke> strokes;
	Point2D cursorCurrentPosition;
	Point2D cursorPreviousPosition;
	
	public TranslateStrokesCommand(Drawing drawing, ArrayList<Stroke> strokes, Point2D cursorCurrentPosition, Point2D cursorPreviousPosition) {
		super(drawing);
		this.strokes = strokes;
		this.cursorCurrentPosition = cursorCurrentPosition;
		this.cursorPreviousPosition = cursorPreviousPosition;
	}
	
	@Override
	public void execute() {
		// compute translation vector
		Vector2D translationVector = Point2D.diff(this.cursorCurrentPosition, this.cursorPreviousPosition);

		// apply the translation to the selected strokes
		for ( Stroke s : this.strokes ) {
			for ( Point2D p : s.getPoints() ) {
				p.copy( Point2D.sum(p,translationVector) );
			}
			s.markBoundingRectangleDirty();
		}
		drawing.markBoundingRectangleDirty();
	}

	@Override
	public void undo() {
		// compute translation vector
		Vector2D translationVector = Point2D.diff(this.cursorPreviousPosition, this.cursorCurrentPosition);

		// apply the translation to the selected strokes
		for ( Stroke s : this.strokes ) {
			for ( Point2D p : s.getPoints() ) {
				p.copy( Point2D.sum(p,translationVector) );
			}
			s.markBoundingRectangleDirty();
		}
		drawing.markBoundingRectangleDirty();
	}
	
	public Point2D getCursorPreviousPosition() {
		return this.cursorPreviousPosition;
	}
	
	public void setCursorPreviousPosition(Point2D cursorPreviousPosition) {
		this.cursorPreviousPosition = cursorPreviousPosition;
	}
}
